<?php
namespace Api\Controller;
use Portal\Controller\CommonController;
class DestroyController extends CommonController {
	function qrcodecard(){
		$post = $this->objectToArray(json_decode(file_get_contents('php://input')));
		if(!$post['Id'] || !$post['Secret'] || !$post['Code']){
			$json['error'] = '请传入Id、Secrety、Code';
		}else{
			$qusers = D('QrcodeUsers')->where(array('name'=>$post['Id']))->find();
			if(!$qusers || $qusers['secret'] != $post['Secret']){
				$json['error'] = '请核对Id与Secrety';
			}else{
				if($qusers['status']==1){
					$card_num = D('CardsList')->where(array('card_number'=>$post['Code']))->find();
					$json = D('Card')->where(array('id'=>$card_num['card_id']))->find();
				}else{
					$json['error'] = '当前账号尚不可用';
				}
			}
		}
		echo json_encode($json);
	}


	function destroycard(){
		$post = $this->objectToArray(json_decode(file_get_contents('php://input')));
		if(!$post['Id'] || !$post['Secret'] || !$post['Code'] || !$post['Cid']){
			$json['error'] = '请传入Id、Secrety、Code、Cid';
		}else{
			$qusers = D('QrcodeUsers')->where(array('name'=>$post['Id']))->find();
			if(!$qusers || $qusers['secret'] != $post['Secret']){
				$json['error'] = '请核对Id与Secrety';
			}else{
				if($qusers['status']==1){
					$card = \Library\CardCode::getInstance();
					if(!$card->checkConsume($post['Cid'], $post['Code'])){
						$json['error'] = '卡券已使用过';
					}else{
						if ($card->consume($post['Cid'], $post['Code'])) {
							$json['error'] = '卡券核销成功!';
		    			}else{
							$json['error'] = '卡券核销失败';
		    			}
					}
				}else{
					$json['error'] = '当前账号尚不可用';
				}
			}
		}
		echo json_encode($json);
	}
	private function objectToArray($data) {
		if(is_object($data)){
			$data = get_object_vars($data);
		}
		if(is_array($data)){
			return array_map(null,$data);
		}
		else{
			return $data;
		}
	}
	function test(){
//		$this->qrcodeusers = D("QrcodeUsers");
//		$this->qrcodeusers->where(array('name'=>'meiguan'))->save(array('request'=>array("exp","request+1")));
//		$Signature = str_replace('%','',urlencode(base64_encode(hash_hmac('sha1', 'bbt-app-hexiao',C('LOCAL_URL') ,true))));
//		echo $Signature;
		$url = 'http://wxtest.baobaot.com/index.php?g=api&m=Destroy&a=destroycard';
		$postjson = json_encode(array('Id'=>'9173','Secret'=>'XzL5BELbVidVCLFe2FwdHLxesgpA3D','Cid'=>'153','Code'=>'955365349071'));
		$e = $this->http_post_json($url,$postjson);
		echo $e[1];
		dump($e);
	}
}
